# -*- encoding: utf-8 -*-


def merge_sort_v1(collection):
    def merge(left, right):
        result = []
        while left and right:
            result.append((left if left[0] <= right[0] else right).pop(0))
        return result + left + right
    # 递归终止条件
    if len(collection) <= 1:
        return collection
    mid = len(collection) // 2
    # 递归公式
    return merge(merge_sort_v1(collection[:mid]), merge_sort_v1(collection[mid:]))


def merge_sort_v2(collection):
    start, end = [], []
    while len(collection) > 1:
        max_one, min_one = max(collection), min(collection)
        start.append(min_one)
        end.append(max_one)
        collection.remove(min_one)
        collection.remove(max_one)
    end.reverse()
    return start + end + collection


if __name__ == '__main__':
    print(merge_sort_v1([9, 1, 2, 6, 8, 3, 7, 6, 5, 0]))
    print(merge_sort_v2([9, 1, 2, 6, 8, 3, 7, 6, 5, 0]))
